package com.example.demo.Controller;


import com.example.demo.Service.UserService;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/u")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/getall")
    public List<User> getAll(){
        return userService.getAll();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Integer getAdd(@RequestBody User user){
        return userService.getAdd(user);
    }


    @RequestMapping(value = "/add2" ,method = RequestMethod.POST)
    public Integer getAdd2(@RequestBody User user){
        System.out.println(user);
        userService.getAdd2(user);
        // 默认情况下 返回的是受影响的行数， 也可以是别的返回值
        return user.getId();
    }

//    修改操作
    @RequestMapping("/update")
    public Integer update(Integer id,String name){
        return userService.update(id, name);
    }

    @RequestMapping("/del")
    public Integer delById(Integer id){
        if (id==null){
            return -1;
        }
        System.out.println("要被删除的对象id为："+id);
        return userService.delById(id);
    }


    @RequestMapping("/getuser")
    public User getUserById(Integer id){
        return userService.getUserById(id);
    }

    @RequestMapping("/isLogin")
    User isLogin(String name, String pwd){
        System.out.println("name: "+name);
        System.out.println("pwd: "+pwd);
        return userService.isLogin(name,pwd);
    }


    @RequestMapping("/getArtById")
    public ArticleInfo getArtById(Integer id){
        return userService.getArtById(id);
    }






}
